package com.spi.soconversion;

import java.sql.SQLException;

import org.apache.log4j.PropertyConfigurator;

import com.spi.soconversion.base.SOCException;
import com.spi.soconversion.util.ErrorConstants;
import com.spi.soconversion.util.ErrorMessageReader;
import com.spi.soconversion.util.SOCConstants;

/**
 * Main class for Sales Order Conversion program
 * 
 * @author XSPID41
 *
 */
public class SOConversion {
		
    static private org.apache.commons.logging.Log log = 
        org.apache.commons.logging.LogFactory.getLog(SOConversion.class);
       
    private String[] args = null;
	
    /**
     * Main Program
     * 
     * @param args
     * @throws SOCException
     * @throws SQLException
     */
	public static void main(String[] args) throws SOCException, SQLException {
		PropertyConfigurator.configure("log4j.properties");	
		log.debug("Starting Sales Order Conversion...");
		SOConversion main = new SOConversion();		    	
		main.args = args;

		if (args == null) {
			throw new SOCException(ErrorMessageReader
					.getError(ErrorConstants.MISSING_INPUT_PARAMETERS));
		}
		if (args[0] == null) {
			throw new SOCException(
					ErrorMessageReader
					.getError(ErrorConstants.MISSING_CONFIG_PROPERTY_FILE));
		}
		if (!args[0].endsWith(".properties")) {
			throw new SOCException(
					ErrorMessageReader
					.getError(ErrorConstants.INVALID_CONFIG_PROPERTY_FILE));
		} 

		if (args.length < 2) {
			throw new SOCException(
					ErrorMessageReader
					.getError(ErrorConstants.MISSING_ARGUMENT));						
		}
		
		if (!args[1].equalsIgnoreCase("-ib") && 
				!args[1].equalsIgnoreCase("-cso") && 
					!args[1].equalsIgnoreCase("-oso")) {
			throw new SOCException(
					ErrorMessageReader
					.getError(ErrorConstants.MISSING_ARGUMENT));			
		}

		if (args[1].equalsIgnoreCase("-ib")) {
			// Run InstallBase
			main.run(SOCConstants.SOC_INSTALL_BASE); 
		} else if (args[1].equalsIgnoreCase("-cso")) {
			// Run ClosedSalesOrder
			main.run(SOCConstants.SOC_CLOSED_SALES_ORDER);
		} else if (args[1].equalsIgnoreCase("-oso")) {
			// Run OpenSalesOrder
			main.run(SOCConstants.SOC_OPEN_SALES_ORDER);
		}
	}
		
	/**
	 * Execute the respective conversion program based on the given parameters
	 * 	
	 * @param type
	 * @throws SOCException
	 * @throws SQLException
	 */
	public void run(int type) throws SOCException, SQLException {
		IController controller = null;
		
		switch(type) {		
			case SOCConstants.SOC_INSTALL_BASE:
				log.debug("Running Install Base Conversion now...");
				// instantiate install base controller here.
		        try {
		        	InstallBaseController ibController = new InstallBaseController();		        	
		        	ibController.init(args[0]);
		        	ibController.fetch();
		        	ibController.process();           		          
		        }
		        catch (SOCException soe) {
		        	log.error(SOCConstants.NEW_LINE
		                    + SOCConstants.CONVERSION_NOT_PERFORM_INSTALL_BASE
		                    + SOCConstants.NEW_LINE
		                    + soe.getMessage());
		            System.exit(1);
		        }
				
				break;
			case SOCConstants.SOC_OPEN_SALES_ORDER:
				log.debug("Running Open Sales Order Conversion now...");
				// instantiate open sales order controller here
				try {
					OpenSalesOrderController osController = new OpenSalesOrderController();
					osController.init(args[0]);
					osController.fetch();
					osController.process();
					osController.write();
					osController.update();
				} catch (SOCException soe) {
						soe.printStackTrace();
						log.error(SOCConstants.NEW_LINE
								+ SOCConstants.CONVERSION_NOT_PERFORM_OPEN_SALES_ORDER
								+ SOCConstants.NEW_LINE
								+ soe.getMessage());
						System.exit(1);
				}
				
				break;
			case SOCConstants.SOC_CLOSED_SALES_ORDER:
				log.debug("Running Closed Sales Order Conversion now...");
				// instantiate closed order controller here.				
				try {
					ClosedSalesController csController = new ClosedSalesController();
					csController.init(args[0]);
					csController.fetch();	
					csController.process(); 

				} catch (SOCException soe) {

					log.error(SOCConstants.NEW_LINE
							+ SOCConstants.CONVERSION_NOT_PERFORM_CLOSED_SALES_ORDER
							+ SOCConstants.NEW_LINE
							+ soe.getMessage());
					soe.printStackTrace();
					System.exit(1);
				}
				
				break;
			default:
				break;
		}	
		
		System.exit(0);
		log.debug("Sales Order Conversion has been Completed.");		
	}
}